/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.daten;



/**
 * @author Michael Nitsche
 */
public class WandleObjekt {

	/**
	 * Wandelt das Objekt so einfach und direkt wie möglich in Text um
	 */
	public static String zuText(Object o) {
		if(o==null)
			return "null";
		if(o instanceof String)
			return (String)o;
		return o.toString();
	}

	/**
	 * Wie Debug, nur daß bei einem einfachen String auch ein String zurück kommt!
	 */
	public static String zuTextAusgabe(Object o) {
		if(o==null)
			return "null";
		if(o instanceof String && (""+o).length()>0)
			return ""+o;
		return zuTextDebug(o);
	}

	/**
	 * Das Objekt wird möglichst eindeutig, gut erkennbar und klar abgegrenzt zurückgegeben
	 * char werden mit (char) vorne weg dargestellt
	 */
	public static String zuTextDebug(Object o) {
		if(o==null) return "null";
		if(o.getClass().isArray()) {
			if(o instanceof boolean[]) return zuTextDebug((boolean[])o);
			if(o instanceof byte[])    return zuTextDebug((byte[])o);
			if(o instanceof char[])    return zuTextDebug((char[])o);
			if(o instanceof short[])   return zuTextDebug((short[])o);
			if(o instanceof int[])     return zuTextDebug((int[])o);
			if(o instanceof long[])    return zuTextDebug((long[])o);
			if(o instanceof float[])   return zuTextDebug((float[])o);
			if(o instanceof double[])  return zuTextDebug((double[])o);
			return zuTextDebug((Object[])o);
		}
		if(o instanceof Boolean)   return ""+o;
		if(o instanceof Byte)      return "(byte)"+o;
		if(o instanceof Character) return zuTextDebug(((Character)o).charValue());
		if(o instanceof Short)     return "(short)"+o;
		if(o instanceof Integer)   return ""+o;
		if(o instanceof Long)      return ""+o+"L";
		if(o instanceof Float)     return ""+o+"F";
		if(o instanceof Double)    return ""+o+"D";

		if(o instanceof String) return "\""+o+"\"";

		if(o instanceof Class<?>) return (((Class<?>)o).isInterface() ? "(Interface)" : "(Class)")+((Class<?>)o).getSimpleName(); //Klasse.gName(o)
		if(o.getClass() == Object.class) return "Objekt";

		return o.toString();
	}

	public static String zuTextDebug(boolean[] r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+r[i]); return "boolean["+b.substring(1)+"]"; }
	public static String zuTextDebug(byte[]    r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+r[i]); return "byte["+b.substring(1)+"]"; }
	public static String zuTextDebug(short[]   r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+r[i]); return "short["+b.substring(1)+"]"; }
	public static String zuTextDebug(int[]     r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+r[i]); return "int["+b.substring(1)+"]"; }
	public static String zuTextDebug(long[]    r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+zuTextDebug(r[i])); return "long["+b.substring(1)+"]"; }
	public static String zuTextDebug(float[]   r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+zuTextDebug(r[i])); return "float["+b.substring(1)+"]"; }
	public static String zuTextDebug(double[]  r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+zuTextDebug(r[i])); return "double["+b.substring(1)+"]"; }
	public static String zuTextDebug(char[]    r)   { StringBuffer b=new StringBuffer(); for(int i=0; i<r.length; i++) b.append(","+zuTextDebug(r[i])); return "char["+b.substring(1)+"]"; }

	public static String zuTextDebug(Object[] bereich) {
		String typ=bereich.getClass().toString().replaceFirst("^.*\\.([A-Za-z_]+);.*","$1");
		StringBuffer erg = new StringBuffer(typ+"(");
		for(int i=0; i<bereich.length; i++)
			erg.append("\n   "+zuTextDebug(bereich[i])+(i==bereich.length-1? "" :","));
		String erg2 = erg.toString();
		return (erg2.length()<100 ? erg2.replaceAll("\n   ","") : erg2)+")";
	}

	public static String zuTextDebug(char c)   { return c>31 && c<127 ? "(char)"+c :"(char)"+(int)c; }

}
